﻿package com.ghost.utils
{
	import flash.display.InteractiveObject;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;

	/**
	 * 提示文本
	 */
	public class AS3ToolTip {

		
		//目标对象数组
		static public  var tips : Array = new  Array();

		public function AS3ToolTip() {
		}
		/**
		 * 新建一个提示文本
		 *
		 * @param   owner  要设置提示文本的目标对象
		 * @param   text  提示文本的内容
		 */
		public function create(owner : InteractiveObject, text : String) : void {
			Create(owner, text);
		}

		/**
		 * 新建一个提示文本
		 *
		 * @param   owner  要设置提示文本的目标对象
		 * @param   text  提示文本的内容
		 */

		static public  function Create(owner : InteractiveObject, text : String) : void {
			for(var a in tips) {
				if(tips[a].owner == owner) {
					tips[a].text = text;
					return;	
				}	
			}
			
			var toolTip : MyTip = new MyTip(owner, text);
			tips.push(toolTip);
			var showToolFunction : Function = function(e : MouseEvent):void {
				if(owner.stage == null) {
					return;	
				}
				toolTip.name = "tooltip";
				owner.stage.addChild(toolTip);
				owner.addEventListener(MouseEvent.MOUSE_OUT, hideToolFunction);
				owner.removeEventListener(MouseEvent.MOUSE_OVER, showToolFunction);
			};
			var hideToolFunction : Function = function():void {
				if(owner.stage == null) {
					toolTip = null;
					return;	
				}
				owner.stage.removeChild(toolTip); 
				owner.addEventListener(MouseEvent.MOUSE_OVER, showToolFunction);
				owner.removeEventListener(MouseEvent.MOUSE_OUT, hideToolFunction);
			};
			owner.addEventListener(MouseEvent.MOUSE_OVER, showToolFunction);
		}
	}
}

import flash.display.Sprite;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.text.TextFormat;
import flash.display.InteractiveObject;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.filters.GlowFilter;

class MyTip extends Sprite {
	public var owner : InteractiveObject;
	private var toolTip : TextField;
	private var _text : String;

	public function get text() : String {
		return _text;	
	}

	public function set text(_s : String) : void {
		_text = _s;	
		toolTip.htmlText = _s;
	}

	
	public function MyTip(_ower : InteractiveObject ,txt : String) : void {
		owner = _ower;
		_text = txt;
		this.addEventListener(Event.ADDED_TO_STAGE, listener);
		this.addEventListener(Event.REMOVED_FROM_STAGE, relistener);
		toolTip = new TextField();
		toolTip.visible = true;
		toolTip.textColor = 0xffffff;
		toolTip.background = true;
		toolTip.backgroundColor = 0x272523;
		toolTip.border = true;
		toolTip.borderColor = 0x000000;
		toolTip.multiline = true;
		//toolTip.wordWrap = true;
		toolTip.autoSize = TextFieldAutoSize.LEFT;
		toolTip.selectable = false;
		toolTip.mouseEnabled = false;
		var 包边颜色:Number = 0xffff00;
		var 透明度:Number = 1;
		var 水平模糊量:Number =1.3
		var 垂直模糊量:Number =1.3
		var 散布强度:Number =2
		var 品质:Number =1
		toolTip.filters = [new GlowFilter(包边颜色, 透明度, 水平模糊量, 垂直模糊量, 散布强度, 品质)];
		//设置动态文本样式
		var format : TextFormat = new TextFormat();
		format.font = "_sans";
		format.leftMargin = 0;
		format.rightMargin = 0;
		//format.color = 0x000000;
		format.size = 12;
		toolTip.setTextFormat(format);
		toolTip.htmlText = txt;
		if(toolTip.textWidth >=125)
		{
			toolTip.wordWrap = true;
		}
		this.addChild(toolTip);
		this.alpha = 0.5;
	}

	private function relistener(e : Event) : void {
		
		stage.removeEventListener(MouseEvent.MOUSE_MOVE, onMouseMove1);	
	}

	private function listener(e : Event) : void {
		//this.x = this.parent.mouseX +this.toolTip.width/2;
		this.x = this.parent.mouseX +10;
		this.y = this.parent.mouseY + 10;
		stage.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove1);
	}

	private function onMouseMove1(e : MouseEvent) : void {
		if(owner.stage != null)
		{
			this.x = this.parent.mouseX +10;
			this.y = this.parent.mouseY + 10;
		}else
		{
			this.parent.removeChild(this);				
			//stage.removeEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
		}
		if(this.parent != null && stage != null)
		{
			if((this.parent.mouseX+this.toolTip.width)>=(stage.stageWidth-this.toolTip.width/2))
			{
				this.x = this.parent.mouseX -this.toolTip.width;
			}
			else
			{
				if((this.parent.mouseY)>=(stage.stageHeight-this.toolTip.height))
				{
					this.y = this.parent.mouseY-10;
				}
			}
		}
	}
}
